home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Alles Voor Internet / Tout Pour Internet
/
alles voor internet.iso
/
MacInternet™
/
Telnet
/
NCSA
/
tn3270 2.4d7 source
/
tn3270
/
serscrn.c
< prev
next >
Wrap
Text File
|
1992-04-17
|
14KB
|
541 lines
/*
* tn3270 for the Macintosh Source Code
* Brown University Computing and Information Services
* Version 2.4d7 April, 1992
* Copyright (c) 1988, 1989, 1990, 1991, 1992 by Brown University and by
* Peter John DiCamillo.
*
* Permission is granted to any individual or institution to use, copy,
* or redistribute the binary version of this software and its
* documentation provided this notice and the copyright notices are
* retained. Permission is granted to any individual or non-profit
* institution to use, copy, modify, or redistribute the source files
* of this software provided this notice and the copyright notices are
* retained. This software may not be distributed for profit, either
* in original form or in derivative works, nor can the source be
* distributed to other than an individual or a non-profit institution.
* Any individual or group interested in seeing and/or using these
* source files but who are prevented from doing so by the above
* constraints should contact Don Wolfe, Assistant Vice-President for
* Computer Systems at Brown University, (401) 863-7250, for possible
* software licensing of the source developed at Brown.
*
* Brown University and Peter John DiCamillo make no representations
* about the suitability of this software for any purpose.
*
* BROWN UNIVERSITY AND PETER JOHN DICAMILLO GIVE NO WARRANTY, EITHER
* EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
* INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
*
*/
#if !defined(USEDUMP)
#include "maclib.h"
#include "termdef.h"
#include "tn3270funcs.h"
#include "globals.h"
#else
#pragma load "tn3270DumpFile"
#endif
#pragma segment 3270seg3
extern RGBColor realwhite;
unsigned char cptoebc[] = {
0x00,0x01,0x02,0x03,0x37,0x2d,0x2e,0x2f,
0x16,0x05,0x25,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x3b,0x3c,0x3d,0x32,0x26,
0x18,0x19,0x3f,0x27,0x1c,0x1d,0x1e,0x1f,
0x40,0x5a,0x7f,0x7b,0x5b,0x6c,0x50,0x7d,
0x4d,0x5d,0x5c,0x4e,0x6b,0x60,0x4b,0x61,
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
0xf8,0xf9,0x7a,0x5e,0x4c,0x7e,0x6e,0x6f,
0x7c,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,
0xc8,0xc9,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,
0xd7,0xd8,0xd9,0xe2,0xe3,0xe4,0xe5,0xe6,
0xe7,0xe8,0xe9,0xad,0xe0,0xbd,0x5f,0x6d,
0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
0x97,0x98,0x99,0xa2,0xa3,0xa4,0xa5,0xa6,
0xa7,0xa8,0xa9,0xc0,0x4f,0xd0,0xa1,0x07};
void putscr(unsigned char *s, short slen, char wr, cnr *cp)
{
static unsigned char idstr[] = {0x1b, 0x2f, 0x5a};
register unsigned char a, c;
register short count;
short i, m, newadr;
char writeflg;
unsigned char saverow;
if (slen == 0) return;
writeflg = 0;
newadr = cp->curadr;
for (count=0; count < slen; count++) {
a = s[count];
if (wr) serout(a);
c = cptoebc[a & 0x7f];
switch(cp->escmode) {
case 0: /* no preceding ESC */
if (c >= 0x40) cp->skiplf = 0;
switch(c) {
case 0x2f: /* bell (07) */
cp->pndbeep = 1;
break;
case 0x16: /* backspace (08) */
if (newadr == 0) newadr = cp->maxoff;
else newadr--;
break;
case 0x05: /* horizontal tab (09) */
newadr >> 2;
newadr += 1;
newadr << 2;
if (newadr > cp->maxoff) newadr = 0;
break;
case 0x25: /* line feed (0A) */
if (cp->skiplf) {
cp->skiplf = 0;
break;
}
newadr = (newadr/cp->scrhsize) * cp->scrhsize;
m = cp->maxoff/cp->scrhsize;
i = newadr/cp->scrhsize;
if (i < m) newadr += cp->scrhsize;
else {
scrollscrn(cp);
writeflg = 0;
}
break;
case 0x0b: /* vertical tab (0B) */
newadr += cp->scrhsize;
if (newadr > cp->maxoff) newadr -= cp->maxcnt;
break;
case 0x0c: /* form feed (0C) */
clrscn(cp);
invldscr(cp);
newadr = 0;
break;
case 0x0d: /* carriage return (0D) */
newadr = (newadr/cp->scrhsize) * cp->scrhsize;
break;
case 0x27: /* ESC */
cp->escmode = 1;
default:
if (c < 0x40) break; /* ignore other control chars. */
/* data character */
if ((c == 0xad) || (c == 0xbd)) {
newchar(newadr, c, 0x4000, 0, cp);
}
else {
newchar(newadr, c, 0, 0, cp);
}
writeflg = 1;
newadr++;
if ((newadr%cp->scrhsize) == 0) cp->skiplf = 1;
if (newadr == cp->maxcnt) {
newadr = (cp->maxoff/cp->scrhsize) * cp->scrhsize;
scrollscrn(cp);
writeflg = 0;
}
break;
}
break;
case 1: /* first char. after ESC */
switch(c) {
case 0xc1: /* A - up */
if ((newadr-cp->scrhsize) >= 0) newadr -= cp->scrhsize;
cp->escmode = 0;
break;
case 0xc2: /* B - down */
if ((newadr+cp->scrhsize) < cp->maxcnt) newadr += cp->scrhsize;
cp->escmode = 0;
break;
case 0xc3: /* C - right */
if ((newadr+1) < cp->maxcnt) newadr++;
cp->escmode = 0;
break;
case 0xc4: /* D - left */
if ((newadr-1) >= 0) newadr--;
cp->escmode = 0;
break;
case 0xc8: /* H - cursor home */
newadr = 0;
cp->escmode = 0;
break;
case 0xc9: /* I - reverse line feed */
if (newadr > cp->scrhoff) newadr -= cp->scrhsize;
else {
revscroll(cp);
writeflg = 0;
}
cp->escmode = 0;
break;
case 0xd1: /* J - erase to end of screen */
i = newadr;
newchar(i, 0, 0, 0, cp);
i++;
while (i < cp->maxcnt) {
newchar(i, 0, 0, 0, cp);
i++;
}
writeflg = 1;
cp->escmode = 0;
break;
case 0xd2: /* K - erase to end of line */
i = newadr;
newchar(i, 0, 0, 0, cp);
i++;
while ((i < cp->maxcnt) && ((i%cp->scrhsize) != 0)) {
newchar(i, 0, 0, 0, cp);
i++;
}
writeflg = 1;
cp->escmode = 0;
break;
case 0xe8: /* Y - start of cursor address */
cp->escmode = 2;
break;
case 0xe9: /* Z - return my terminal type */
if (cp->tcpflg) {
tcpwrite(idstr, 3, cp);
}
else if (cp->serflg) {
serout(idstr[0]);
serout(idstr[1]);
serout(idstr[2]);
}
cp->escmode = 0;
break;
default:
cp->escmode = 0;
break;
}
break;
case 2: /* second char. after ESC */
saverow = a;
cp->escmode = 3;
break;
case 3: /* third char. after ESC */
newadr = (saverow - 0x20) * cp->scrhsize + a - 0x20;
if (newadr < 0) newadr = 0;
if (newadr > cp->maxoff) newadr = cp->maxoff;
cp->escmode = 0;
break;
default:
break;
}
}
if (writeflg) newwrite(cp);
if (cp->curadr != newadr) {
cp->curadr = newadr;
newcur(cp);
}
}
void scrollscrn(cnr *cp)
{
register short i, n;
unsigned char * cptr;
unsigned short * iptr;
/* update character and attribute buffers */
n = cp->maxcnt/cp->scrhsize - 1;
cptr = cp->chrbuff;
iptr = cp->atrbuff;
for (i=0; i < n; i++) {
memcpy(cptr, cptr+cp->scrhsize, cp->scrhsize);
memcpy(iptr, iptr+cp->scrhsize, cp->scrhsize<<1);
cptr += cp->scrhsize;
iptr += cp->scrhsize;
}
memset(cptr, 0, cp->scrhsize);
memset(iptr, 0, cp->scrhsize<<1);
/* prevent drawing of saved input */
cp->in_len = 0;
cp->inrect.top = cp->inrect.left = 32767;
cp->inrect.bottom = cp->inrect.right = -32767;
/* update bitmap or screen */
invldscr(cp);
}
void revscroll(cnr *cp)
{
register short i, n;
unsigned char * cptr;
unsigned short * iptr;
/* update character and attribute buffers */
n = cp->maxcnt/cp->scrhsize - 1;
cptr = cp->chrbuff + cp->maxcnt - cp->scrhsize;
iptr = cp->atrbuff + cp->maxcnt - cp->scrhsize;
for (i=0; i < n; i++) {
memcpy(cptr, cptr-cp->scrhsize, cp->scrhsize);
memcpy(iptr, iptr-cp->scrhsize, cp->scrhsize<<1);
cptr -= cp->scrhsize;
iptr -= cp->scrhsize;
}
memset(cptr, 0, cp->scrhsize);
memset(iptr, 0, cp->scrhsize<<1);
/* prevent drawing of saved input */
cp->in_len = 0;
cp->inrect.top = cp->inrect.left = 32767;
cp->inrect.bottom = cp->inrect.right = -32767;
/* update bitmap or screen */
invldscr(cp);
}
void putsrv(unsigned char *s, short slen, char inp, cnr *cp)
{
register unsigned char a, c;
short count, i, len, newadr, startadr;
char writeflg;
unsigned char *cptr;
if (slen == 0) return;
writeflg = 0;
newadr = cp->curadr;
startadr = cp->maxcnt - cp->scrhsize * 2;
for (count=0; count < slen; count++) {
a = s[count];
c = cptoebc[a & 0x7f];
if (!inp) {
srvnewchar(c, cp);
continue;
}
switch(c) {
case 0x16: /* backspace (08) */
if (newadr == startadr) break;
newadr--;
newchar(newadr, 0, 0, 0, cp);
writeflg = 1;
break;
case 0x0d: /* carriage return (0D) */
cptr = cp->chrbuff + cp->maxoff;
for (i = cp->scrhsize*2; i > 0; i--) {
a = *(cptr--);
if ((a != 0) && (a != 0x40)) break;
}
len = i;
newadr = startadr;
if (len == 0) break;
if (cp->serflg) {
cp->respflag = 1;
cptr = cp->chrbuff + startadr;
for (i = 0; i < len; i++) {
serout(((*xtabh)[cptr[i]]) & 0x7f);
}
serout(0x0d);
}
else {
srvtext(cp->chrbuff + startadr, cp->atrbuff + startadr, len, cp);
cptr = cp->chrbuff + startadr;
for (i = 0; i < len; i++) {
cptr[i] = (*xtabh)[cptr[i]];
}
cptr[len++] = 0x0d;
cptr[len++] = 0x0a;
tcpwrite(cptr, len, cp);
}
memset(cp->chrbuff + startadr, 0, cp->scrhsize*2);
memset(cp->atrbuff + startadr, 0, cp->scrhsize*4);
/* prevent drawing of saved input */
cp->in_len = 0;
cp->inrect.top = cp->inrect.left = 32767;
cp->inrect.bottom = cp->inrect.right = -32767;
/* update bitmap or screen */
invldscr(cp);
break;
default:
if (c < 0x40) break; /* ignore other control chars. */
/* data character */
if (newadr == cp->maxoff) {
beep(cp);
break;
}
if ((c == 0xad) || (c == 0xbd)) newchar(newadr, c, 0x4006, 6, cp);
else newchar(newadr, c, 6, 6, cp);
writeflg = 1;
newadr++;
break;
}
}
if (writeflg) newwrite(cp);
if (cp->curadr != newadr) {
cp->curadr = newadr;
newcur(cp);
}
}
void srvnewchar(unsigned char c, cnr *cp)
{
int rc;
switch(c) {
case 0x25:
rc = srvproc(cp->linetext, cp->lineattr, cp->linelen, cp);
if (rc == 0) srvtext(cp->linetext, cp->lineattr, cp->linelen, cp);
cp->linelen = 0;
break;
default:
if (c < 0x40) break;
if (cp->linelen == 512) break;
(cp->linetext)[cp->linelen] = c;
if ((c == 0xad) || (c == 0xbd)) (cp->lineattr)[cp->linelen++] = 0x4000;
else (cp->lineattr)[cp->linelen++] = 0;
break;
}
}
void srvtext(unsigned char *text, unsigned short *attr, short len, cnr *cp)
{
short i, linecount, scrollcount;
if (len == 0) return;
linecount = (len + cp->scrhsize - 1)/cp->scrhsize;
scrollcount = linecount - (cp->maxcnt - cp->scrhsize*2 - cp->msgoffset)/cp->scrhsize;
if (scrollcount > 0) {
for (i = 0; i < scrollcount; i++) {
srvscroll(cp);
cp->msgoffset -= cp->scrhsize;
}
}
if (((attr[0] & 0x07) == 6) || cp->respflag) {
for (i = 0; i < len; i++) {
attr[i] &= 0xFFF8;
attr[i] |= 5;
}
cp->respflag = 0;
}
memcpy(cp->chrbuff+cp->msgoffset, text, len);
memcpy(cp->atrbuff+cp->msgoffset, attr, len<<1);
cp->msgoffset += linecount*cp->scrhsize;
/* prevent drawing of saved input */
cp->in_len = 0;
cp->inrect.top = cp->inrect.left = 32767;
cp->inrect.bottom = cp->inrect.right = -32767;
/* update bitmap or screen */
invldscr(cp);
}
int srvproc(unsigned char *text, unsigned short *attr, short len, cnr *cp)
{
short i;
unsigned char *irchost;
short ircsize;
if (len == 0) return(0);
if (cp->serverflags == 0) return(0);
memcpy(cp->proctext, text, len);
memcpy(cp->procattr, attr, len<<1);
/* IRC processing */
if (len < 5) return(0);
for (i = 0; i < 5; i++) {
(cp->proctext)[i] = (*xtabh)[(cp->proctext)[i]];
}
(cp->proctext)[5] = 0;
if (strcmp(cp->proctext, "PING ") == 0) {
if (cp->hostok == 0) {
cp->hostok = 1;
strcpy(cp->prochost, "PONG ");
irchost = cp->connhostname;
while ((*irchost != '\0') && (*irchost != ':')) irchost++;
ircsize = 0;
if (*irchost == ':') {
irchost++;
while ((irchost[ircsize] != '\0') &&
(irchost[ircsize] != ':')) ircsize++;
}
if (ircsize > 0) {
for (i=0; i < ircsize; i++) {
(cp->prochost)[i+5] = tolower(irchost[i]);
}
(cp->prochost)[ircsize+5] = 0;
}
else (cp->prochost)[4] = 0;
i = strlen(cp->prochost);
if (cp->serflg) {
(cp->prochost)[i] = 0x0d;
}
else {
(cp->prochost)[i++] = 0x0d;
(cp->prochost)[i] = 0x0a;
}
}
if (cp->serflg) {
i = 0;
while ((cp->prochost)[i] != 0) {
serout((cp->prochost)[i++]);
}
cp->respflag = 1;
return(1);
}
else {
tcpwrite(cp->prochost, strlen(cp->prochost), cp);
return(0);
}
}
return(0);
}
void srvscroll(cnr *cp)
{
register short i, n;
unsigned char * cptr;
unsigned short * iptr;
/* update character and attribute buffers */
n = cp->maxcnt/cp->scrhsize - 1;
n -= 2;
cptr = cp->chrbuff;
iptr = cp->atrbuff;
for (i=0; i < n; i++) {
memcpy(cptr, cptr+cp->scrhsize, cp->scrhsize);
memcpy(iptr, iptr+cp->scrhsize, cp->scrhsize<<1);
cptr += cp->scrhsize;
iptr += cp->scrhsize;
}
memset(cptr, 0, cp->scrhsize);
memset(iptr, 0, cp->scrhsize<<1);
}
void srvswitch(cnr *cp)
{
cp->servermode ^= 1;
if (cp->servermode) {
cp->curadr = cp->maxcnt - cp->scrhsize*2;
cp->msgoffset = 0;
cp->linelen = 0;
cp->linetext = cp->readbuff;
cp->lineattr = (unsigned short *)(cp->linetext + 512);
cp->proctext = cp->linetext + 1536;
cp->procattr = (unsigned short *)(cp->linetext + 2048);
cp->prochost = cp->linetext + 3072;
cp->respflag = 0;
}
else {
cp->curadr = 0;
}
memset(cp->chrbuff, 0, cp->maxcnt);
memset(cp->atrbuff, 0, cp->maxcnt<<1);
/* prevent drawing of saved input */
cp->in_len = 0;
cp->inrect.top = cp->inrect.left = 32767;
cp->inrect.bottom = cp->inrect.right = -32767;
/* update bitmap or screen */
invldscr(cp);
newcur(cp);
}